package com.wanli.storemobile.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.google.android.flexbox.FlexboxLayout;
import com.wanli.storemobile.R;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.bean.ProductClassifyListBean;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.utils.Dp2Px;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.ViewLoading;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author:HWC
 */
public class ChooseClassifyDialog extends DialogFragment {


    private Activity mActivity;
    private Dialog dialog;
    @BindView(R.id.flex_record)
    FlexboxLayout flexRecord;
    private OnCallBackListener onCallBackListener;
    private List<ProductClassifyListBean.DataBean.ItemListBean> classifyList;
    private String id;
    private ProductClassifyListBean.DataBean.ItemListBean itemListBean;
    private TextView selectTv;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_choose_classify, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }


    public static ChooseClassifyDialog newInstance(String id, List<ProductClassifyListBean.DataBean.ItemListBean> classifyList) {
        Bundle args = new Bundle();
        ChooseClassifyDialog fragment = new ChooseClassifyDialog();
        args.putString("id", id);
        args.putSerializable("classifyList", (Serializable) classifyList);
        fragment.setArguments(args);
        return fragment;
    }

    //    初始化请求
    private void initView() {
        id = getArguments().getString("id");
        classifyList = (List<ProductClassifyListBean.DataBean.ItemListBean>) getArguments().getSerializable("classifyList");
        for (ProductClassifyListBean.DataBean.ItemListBean bean : classifyList) {
            if (bean.getId().equals(id)) {
                bean.setCheck(true);
            }
            flexRecord.addView(getFlexView(bean));
        }
    }

    private TextView getFlexView(ProductClassifyListBean.DataBean.ItemListBean bean) {

        TextView tv = new TextView(mActivity);
        tv.setText(bean.getName());
        tv.setTextSize(14);
        int padding = (int) Dp2Px.dp2Px(15);
        tv.setPadding(padding, 0, padding, 0);
        tv.setGravity(Gravity.CENTER);
        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,//
                (int) Dp2Px.dp2Px(28));
        int marginRight = (int) Dp2Px.dp2Px(15);
        int marginTop = (int) Dp2Px.dp2Px(15);
        params.setMargins(0, marginTop, marginRight, 0);

        tv.setLayoutParams(params);
        if (bean.isCheck()) {
            selectTv = tv;
            itemListBean = bean;
            tv.setBackgroundResource(R.drawable.shape_rectangle_conner_theme14);
            tv.setTextColor(getResources().getColor(R.color.white));
        } else {
            tv.setBackgroundResource(R.drawable.shape_rectangle_conner_gary14);
            tv.setTextColor(getResources().getColor(R.color.text_color_2));
        }
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemListBean != null) {
                    itemListBean.setCheck(false);
                }
                bean.setCheck(true);
                itemListBean = bean;

                if (selectTv != null) {
                    selectTv.setBackgroundResource(R.drawable.shape_rectangle_conner_gary14);
                    selectTv.setTextColor(getResources().getColor(R.color.text_color_2));
                }
                tv.setBackgroundResource(R.drawable.shape_rectangle_conner_theme14);
                tv.setTextColor(getResources().getColor(R.color.white));

                selectTv = tv;

                onCallBackListener.onCallBack(bean.getId() + "");
                dismiss();
            }
        });
        return tv;
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) Dp2Px.dp2Px(280);
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.RIGHT;
        window.getAttributes().windowAnimations = R.style.dialog_right;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    public interface OnCallBackListener {
        void onCallBack(String id);
    }
}
